const app = getApp()
import util from '../../../utils/util'
import {
  GetDay,
  GetLocation,
  SetPunch
} from '../../../config/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isClocked: false, // 真实当天是否打卡
    is_h:0, // 是否打过卡
    flag:true, // 打开成功弹层
    grider_storge: '', // 网格员/长信息
    month: '', // 年月
    cur_month:'', // 当前年月
    today:'', // 今天
    cur_day:'', // 当天几号
    days:'',
    location:'', //定位地址
    lntlgt:'', // 经纬度
    clock_time:'', // 打卡时间
    clock_location:'', // 某一天打卡地址
    time:'', // 打卡时间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that =this;
  
    let grider_storge = wx.getStorageSync('grider');
    this.setData({
      grider_storge: grider_storge
    })

    let year_month = this.getYearMonth()

    console.log('年月', year_month)

    this.setData({
      cur_month: year_month,
      month: year_month
    })

    this.getDays(); // 调用获取每月天
    this.getLocationAddr(); // 调用获取定位信息
  },

  getYearMonth() { // 计算当前获取年月
    var myDate = new Date();
    var tYear = myDate.getFullYear();
    var tMonth = myDate.getMonth();
    var cur_day = myDate.getDate()
    console.log('几号：', cur_day)

     this.setData({
       cur_day,
       today: cur_day
     })

    var m = tMonth + 1;
    if (m.toString().length == 1) {
      m = "0" + m;
    }
    return tYear + '-' + m;
  },

  getDays() { // 获取每月天数
    let that = this;

   let cur_day = this.data.cur_day

    console.log('当前天',cur_day)

    let params = {
      uid: that.data.grider_storge.id,
      month: that.data.month
    }
    util.apiRequest(GetDay, params, null).then(r => {
      console.log('打卡天', r)
      that.setData({
        days:r.data.data
      })

      let dayArr = r.data.data
      
      for (let i = 0, len = dayArr.length; i < len; ++i){
        if (parseInt(that.data.cur_day) == parseInt(dayArr[i].day) && dayArr[i].is_h == 1 ){
          that.setData({
            is_h: 1,
            clock_location:dayArr[i].location,
            time: dayArr[i].time
          })
          break;
        }else{
          that.setData({
            is_h: 0
          })
        }
      }

      console.log('初始化clock_location',that.data.clock_location)

      console.log('当天是否打卡', that.data.is_h)
    })
  },

  queryDayClock(e){ // 点击查看某天打卡信息
    let day = e.currentTarget.dataset.day;
    let is_h = e.currentTarget.dataset.is_h;
    let location = e.currentTarget.dataset.location;
    let time = e.currentTarget.dataset.time;

    this.setData({
      cur_day:day,
      is_h,
      clock_location: location,
      time:time
    })
  },

  onPrevMonth() { // 选择上一个月
    console.log('上一个月')
    let month = this.data.month;
    let monthArr = month.split("-")
    console.log('年月数组:',monthArr)
    let now_year = monthArr[0]
    let now_month = monthArr[1]-1
    
    if (now_month.toString().length == 1) {
      now_month = "0" + now_month;
    }

    if (now_month == 0){
      now_month = 12;
      now_year = monthArr[0]-1
    }

    console.log('上一个月', now_month)
    console.log('上一个年', now_year)

    let prev_month = now_year + '-' + now_month;

    this.setData({
      month: prev_month
    })

    console.log('是否是本月',this.data.month == this.data.cur_month)

    this.getDays()

  },

  onNextMonth() { // 选择下一个月
    console.log('下一个月')
    let cur_month = this.data.cur_month;
    let cur_monthArr = cur_month.split("-")
    let cur_date = new Date(cur_month)
    console.log('cur_date:',cur_date)

    let month = this.data.month;
    let monthArr = month.split("-")
    let date = new Date(month)
    console.log('date:', date)


    if (date < cur_date){
      let now_year = monthArr[0]
      let now_month = parseInt(monthArr[1]) + 1

      if (now_month == 13) {
        now_month = 1;
        now_year = parseInt(monthArr[0]) + 1
      }

      if (now_month.toString().length == 1) {
        now_month = "0" + now_month;
      }

      console.log('下一个月', now_month)
      console.log('下一个年', now_year)

      let prev_month = now_year + '-' + now_month;

      this.setData({
        month: prev_month
      })

      console.log('是否是本月', this.data.month == this.data.cur_month)

      this.getDays()
    }
  },

  onClock(){ // 打卡
  let that =this;
    if(!this.data.location){
      wx.showModal({
        showCancel:false,
        content: '请获取定位信息后再打卡',
      })
      return;
    }

    var myDate = new Date();
    let hour = myDate.getHours()
    let minute = myDate.getMinutes()
    if (minute<10){
      minute = '0' + minute
    }

    let clock_time = hour + ':' + minute
    console.log('clock_time:', clock_time)
    
    let params = {
      uid: this.data.grider_storge.id,
      location: this.data.location,
      lntlgt: this.data.lntlgt,
      vollege_id:this.data.grider_storge.vollege_id,
      town_id: this.data.grider_storge.town_id
    }
    util.apiRequest(SetPunch,params).then(r => {
      console.log('打卡结果',r)
      if (r.data.code == 1) {
        that.setData({
          flag: false,
          clock_time
        })
        this.getDays(); // 打卡成功后重新渲染数据
      } else {
        wx.showToast({
          title: r.data.info,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  conceal(){
    this.setData({
      flag:true
    })
  },

  getLocationAddr(){ // 点击获取定位信息
    let that = this;
    wx.getLocation({
      type: "gcj02",
      // altitude: true,
      // isHighAccuracy: true,
      // highAccuracyExpireTime: 5000,
      success(res) {
        console.log('获取当前位置：', res)

        // wx.chooseLocation({
        //   success(res){
        //     console.log('选择地点',res)
        //   }
        // })

        that.setData({
          lntlgt: res.longitude + ',' + res.latitude
        })

        util.apiRequest(GetLocation, { lntlgt: that.data.lntlgt},null).then(r => {
          console.log('获取到地址',r)
          that.setData({
            location: r.data.data.regeocode.formatted_address
          })
        })
        console.log('lntlgt:',that.data.lntlgt)

      }
    })
  }

})